var enflag = 0;//get_langid();

var bConnect = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var max_chn = 16;
var g_lang_id = 0;
var g_b_voip = 0;

NETDVR_PTZ_STOP = 0;
NETDVR_PTZ_MOVE_UP = 1;					//move up
NETDVR_PTZ_MOVE_DOWN = 2;				//move down
NETDVR_PTZ_MOVE_LEFT = 3;				//move left
NETDVR_PTZ_MOVE_RIGHT = 4;				//move right
NETDVR_PTZ_ZOOM_IN = 6;					//zoom in
NETDVR_PTZ_ZOOM_OUT = 7;				//zoom out
NETDVR_PTZ_FOCUS_FARTHER = 8;			//focus farther
NETDVR_PTZ_FOCUS_NEARER = 9;			//focus nearer
NETDVR_PTZ_APER_INCREASE = 10;			//aperture increase
NETDVR_PTZ_APER_DECREASE = 11;			//aperture decrease
NETDVR_PTZ_LIGHT_ON = 12;				//light on
NETDVR_PTZ_LIGHT_OFF = 13;				//light off
NETDVR_PTZ_WIPER_ON = 14;				//wiper on
NETDVR_PTZ_WIPER_OFF = 15;				//wiper off

function init_activex()
{

}

function full(){ 
	ptz_stop() 
	TLNetDvrCtrl.PTZCtrlStop(); 

	window.resizeTo(830 ,730 );
	window.location.href= window.location.href + "?w=800&h=600" ; 
}

function normal(){ 
	ptz_stop() 
	TLNetDvrCtrl.PTZCtrlStop(); 
 
	window.resizeTo(430 ,430 );
	window.location.href= window.location.href.replace("?w=800&h=600","") ; 
}


function detect_plugin(obj_name, item_name)
{
    try
    {
        if(eval(obj_name+"."+item_name) == undefined)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    catch(e)
    {
        return false;
    }
}

function do_login()
{
	g_lang_id = 0;
	if (false == detect_plugin("TLNetDvrCtrl", "LangID"))
	{
		if (0 == g_lang_id)
		{
			alert("控件未安装!");
		}
		else
		{
			alert("Plugin is not installed!");
		}
		return false;
	}
	
	TLNetDvrCtrl.ServerIP =  document.getElementById("ipadds").value;
	TLNetDvrCtrl.ServerType = 1;//1 fpga
	TLNetDvrCtrl.AlarmInNum = 16;
	TLNetDvrCtrl.AlarmOutNum = 4;
	TLNetDvrCtrl.ServerPort   = 8670;//~ServerPort~
	
	TLNetDvrCtrl.UserName = "admin";
	TLNetDvrCtrl.Password = "";
	TLNetDvrCtrl.LangID = g_lang_id;//0 sc 1 eng
	var ret = TLNetDvrCtrl.Login();
	if (true == ret)
	{
		var nettype = 0;//parseInt(document.getElementById("nettype").value);
   
	}
	else
	{
		if (0 == g_lang_id)
		{
			alert("登录失败!" + g_lang_id);
		}
		else
		{
			alert("login failed!");
		}
	}
	
	return false;
}

function gostart(){ 
	do_set_display_wndnum(1)
	do_login(); 
	setTimeout("goset()", 3000); 
}

function goset(){
	control_channel(0); 
}
//属性：short DisplayWndNum 当前显示的窗口的数目(1.4.9.16)
//方法：void SetDisplayWnd(); 应用改变的窗口数目
//	  short GetDisplayNum();
function do_set_display_wndnum(count)
{
	TLNetDvrCtrl.DisplayWndNum = count;
	TLNetDvrCtrl.SetDisplayWnd();
}

function play_video(chn, streamtype)
{
//	alert(streamtype);
	var ret;
	TLNetDvrCtrl.Channel = chn;
	TLNetDvrCtrl.StreamType = streamtype;
	ret = TLNetDvrCtrl.Preview();
	return ret;
}

function stop_video(chn)
{
	var ret;
	TLNetDvrCtrl.Channel = chn;
	ret = TLNetDvrCtrl.Stop();
	return ret;
}

function get_stream_type(chn)
{
	return 0;//parseInt(document.getElementById("streamtype").value);
	//return parseInt(document.getElementById("streamtye"+chn.toString()).value);
}

function control_channel(chn)
{
	var ret;
	
	if (chn > max_chn)
	{
		return;
	}
	
	var playing_channel = TLNetDvrCtrl.Channel;	
	if (0 == bConnect[chn])
	{
		if (-1 != playing_channel)
		{
			//document.getElementById("pic"+playing_channel.toString()).src = "/images/web_unalive.gif";
			bConnect[playing_channel] = 0;
		}
		
		ret = play_video(chn, get_stream_type(chn));
		if (true == ret)
		{
			//document.getElementById("pic"+chn.toString()).src = "/images/web_alive.gif";
			bConnect[chn] = 1;
		}
		else
		{
			if (0 == enflag)
			{
				alert("预览开启失败!");
			}
			else
			{
				alert("Preview error!");
			}
		}
	}
	else
	{
		ret = stop_video(chn);
		if (true == ret)
		{
			//document.getElementById("pic"+chn.toString()).src = "/images/web_unalive.gif";
			bConnect[chn] = 0;
		}
	}
}

function ptz_command(command)
{
	TLNetDvrCtrl.PTZCommand = command;
	TLNetDvrCtrl.PTZCtrlStart();
}

function ptz_move_up()
{
	ptz_command(NETDVR_PTZ_MOVE_UP);
}

function ptz_move_down()
{
	ptz_command(NETDVR_PTZ_MOVE_DOWN);
}

function ptz_move_left()
{
	ptz_command(NETDVR_PTZ_MOVE_LEFT);
}

function ptz_move_right()
{
	ptz_command(NETDVR_PTZ_MOVE_RIGHT);
}

function ptz_aper_decrease()
{
	ptz_command(NETDVR_PTZ_APER_DECREASE);
}

function ptz_aper_increase()
{
	ptz_command(NETDVR_PTZ_APER_INCREASE);
}

function ptz_focus_farther()
{
	ptz_command(NETDVR_PTZ_FOCUS_FARTHER);
}

function ptz_focus_nearer()
{
	ptz_command(NETDVR_PTZ_FOCUS_NEARER);
}

function ptz_zoom_in()
{
	ptz_command(NETDVR_PTZ_ZOOM_IN);
}

function ptz_zoom_out()
{
	ptz_command(NETDVR_PTZ_ZOOM_OUT);
}

function ptz_stop()
{
	//TLNetDvrCtrl.PTZCtrlStop();
	ptz_command(NETDVR_PTZ_STOP);
}

function do_voip(theButton)
{
	var ret;
	if (0 == g_b_voip)
	{
		ret = TLNetDvrCtrl.StartVOIP();
		if (false == ret)
		{
			if (0 == g_lang_id)
			{
				alert("开启语音对讲失败!");
			}
			else
			{
				alert("Start VOIP failed!");
			}	
		}
		else
		{
			if (0 == g_lang_id)
			{
				theButton.value = "停止对讲";
			}
			else
			{
				theButton.value = "Stop VOIP";
			}	
			g_b_voip = 1;
		}
	}
	else
	{
		ret = TLNetDvrCtrl.StopVOIP();
		if (false == ret)
		{
			if (0 == g_lang_id)
			{
				alert("停止语音对讲失败!");
			}
			else
			{
				alert("Stop VOIP failed!");
			}	
		}
		else
		{
			if (0 == g_lang_id)
			{
				theButton.value = "开启对讲";
			}
			else
			{
				theButton.value = "Start VOIP";
			}	
			g_b_voip = 0;
		}
	}
}

function do_configure()
{
	TLNetDvrCtrl.RemoteParaCfg();
}

function do_playback()
{
	TLNetDvrCtrl.PlayBack();
}

function stop_all()
{
	var i, ret;
	for (i = 0; i < max_chn; i++)
	{
		if (1 == bConnect[i])
		{
			ret = stop_video(i);
			if (true == ret)
			{
				//document.getElementById("pic"+i.toString()).src = "/images/web_unalive.gif";
				bConnect[i] = 0;
			}
		}
	}
}
